package scrapygui;

import com.sun.istack.internal.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

class ScreenCopyModel {
    private CMDModel mCmd = new CMDModel();

    List<DeviceBean> queryDevices(@NotNull Consumer<String> error) {
        List<DeviceBean> retList = new ArrayList<>();
        List<String> list = mCmd.exec( "adb devices -l", error );
        String m = "model:";
        for (String s : list) {
            if( s == null || "List of devices attached".contains( s ) ) continue;
            String via = s.substring( 0, s.indexOf(" ") );
            int modelIndex = s.indexOf( m );
            String model = s.substring( modelIndex + m.length(), s.indexOf( " ", modelIndex ) );
            System.out.println( "via >>> " + via + ", model >>> " + model );
            retList.add( new DeviceBean( model, via ) );
        }
        return retList;
    }

    void startScreen(String via, @NotNull Consumer<String> error) {
        if( via == null || "".equals( via.trim() ) ) {
            error.accept( "device not a via." );
            return;
        }
        //启动投屏
        mCmd.exec( "scrcpy -s " + via, error );
    }
}
